<template>
  <div id="popup" class="popup" v-if="popvisible" :style="popsty">
    <el-form :model="event" size="small">
      <el-form-item label="事件编号" :label-width="formLabelWidth">
        <el-input v-model="event.number" placeholder="" disabled />
      </el-form-item>
      <el-form-item label="事件类型" :label-width="formLabelWidth">
        <el-select v-model="event.type" class="m-2" placeholder="Select">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="事件等级" :label-width="formLabelWidth">
        <el-select v-model="event.level" class="m-2" placeholder="Select">
          <el-option
            v-for="item in options3"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发布时间" :label-width="formLabelWidth">
        <el-date-picker
          v-model="event.time"
          type="datetime"
          placeholder=""
          size="small"
          format="YYYY/MM/DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item label="发生地点" :label-width="formLabelWidth">
        <el-input v-model="event.addr" placeholder="请输入发生地点" clearable />
      </el-form-item>
      <el-form-item label="车牌号" :label-width="formLabelWidth">
        <el-input
          v-model="event.insignia"
          placeholder="请输入车牌号"
          clearable
        />
      </el-form-item>
      <el-form-item label="驾驶员" :label-width="formLabelWidth">
        <el-input v-model="event.driver" placeholder="请输入驾驶员" clearable />
      </el-form-item>
      <el-form-item label="处理状态" :label-width="formLabelWidth">
        <el-select v-model="event.statusnew" class="m-2" placeholder="Select">
          <el-option
            v-for="item in options2"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleSub">提交</el-button>
        <el-button @click="handleCancel">取消</el-button>
      </span>
    </el-form>
  </div>
</template>

<script setup>
import { onMounted, ref, watch } from 'vue'
import { useUsersStore } from '@/stores/user'
import { mapActions, storeToRefs } from 'pinia'
import { useGlobalMap } from '@/utils/globalMap.js'
import { Point } from '@/zondy_api/Point.js'
import {delFeature} from '@/zondy_api/DelFeature.js'
import {docLayer} from '@/zondy_libs/index.js'
var map
let popvisible = ref(false)
let popsty = ref({})
let popoffset = ref([0, 0])
var source
var layer
var fid
var position
const formLabelWidth = '70px'
onMounted(() => {
  map = useGlobalMap()
  source = new ol.source.Vector({})
  var style = new ol.style.Style({
    //形状
    image: new ol.style.Icon({
      anchor: [0.3, 0.3],
      anchorOrigin: 'bottom-right',
      anchorXUnits: 'fraction',
      anchorYUnits: 'pixels',
      offsetOrigin: 'top-right',
      offset: [10, 0],
      //图标缩放比例
      scale: 0.5,
      //透明度
      opacity: 1,
      //图标的url
      src: '/src/images/location.png',
    }),
  })
  layer = new ol.layer.Vector({
    source,
    style,
  })
  map.addLayer(layer)
})
const store = useUsersStore()
const { xgsjDialog, activeIndex } = storeToRefs(store)
//表格数据
let event = ref({
  number: '',
  type: '',
  level: 1,
  time: '',
  addr: '',
  insignia: '',
  driver: '',
  statusnew: 0,
})
// 测试数据
const options = [
  {
    value: '碰撞',
    label: '碰撞',
  },
  {
    value: '刮擦',
    label: '刮擦',
  },
  {
    value: '追尾',
    label: '追尾',
  },
  {
    value: '翻车',
    label: '翻车',
  },
  {
    value: '碾压',
    label: '碾压',
  },
  {
    value: '失火',
    label: '失火',
  },
  {
    value: '其他',
    label: '其他',
  },
]
const options2 = [
  {
    value: 0,
    label: '待处理',
  },
  {
    value: 1,
    label: '处理中',
  },
  {
    value: 2,
    label: '已归档',
  },
]
const options3 = [
  {
    value: 1,
    label: '轻微事故',
  },
  {
    value: 2,
    label: '一般事故',
  },
  {
    value: 3,
    label: '重大事故',
  },
  {
    value: 4,
    label: '特大事故',
  },
]
watch(
  () => store.xgsjDialog,
  () => {
    if (store.xgsjDialog) {
      map.on('click', xgsjpop)
    }
  }
)
//修改事件弹窗的方法
const xgsjpop=(evt)=>{
        //点查询
        const positionquery = evt.coordinate
        var service = {
          name: 'guanggu',
          layerId: 2,
        }
        function handleQuery(res) {
          if (res) {
            fid=res.SFEleArray[0].FID
            const eventnew = res.SFEleArray[0].AttValue
            let timenew = eventnew[3].replace('.', '-').replace('.', '-').trim()
            if (timenew.length < 17) {
              timenew = timenew.concat(':00')
            }
            // console.log(timenew)
            console.log(eventnew);
            let levelnew
            let statusn
            levelnew = options3[eventnew[2]].value
            statusn = options2[eventnew[7]].label
            console.log(timenew)
            event.value = {
              number: eventnew[0],
              type: eventnew[1],
              level: levelnew,
              time: timenew,
              addr: eventnew[4],
              insignia: eventnew[5],
              driver: eventnew[6],
              statusnew: statusn,
            }
            position = []
            let position1 = ref(0)
            let position2 = ref(0)
            position[0] = res.SFEleArray[0].bound.xmax
            position[1] = res.SFEleArray[0].bound.ymax
            let positionnew
            positionnew = map.getPixelFromCoordinate(position)
            position1.value = positionnew[0]
            position2.value = positionnew[1]
            popsty.value = {
              left: `${position1.value + 2}px`,
              top: `${position2.value + 37}px`,
            }
            popvisible.value = true

            //监听地图移动
            map.on('moveend', function () {
              positionnew = map.getPixelFromCoordinate(position)
              position1.value = positionnew[0]
              position2.value = positionnew[1]
              popsty.value = {
                left: `${position1.value + 2}px`,
                top: `${position2.value + 37}px`,
              }
            })
            // console.log(res)
            //创建点要素-图片标记
            var point = new ol.Feature({
              geometry: new ol.geom.Point(position),
            })
            source.clear()
            source.addFeature(point)
            map.render()
          }
        }
        Point.query({
          position:positionquery,
          service,
          success: handleQuery,
        })
      }
//修改的方法
function handleSub() {
  let service = {
    name: 'guanggu',
    layerId: 2,
  }
  delFeature({service,fids:fid,docLayer})
  var attr = [
    { type: 'int', key: '事件编号', value: event.value.number },
    { type: 'string', key: '事件类型', value: event.value.type },
    { type: 'int', key: '事件等级', value: event.value.level },
    { type: 'string', key: '发生时间', value: event.value.time },
    { type: 'string', key: '发生地点', value: event.value.addr },
    { type: 'string', key: '车牌号', value: event.value.insignia },
    { type: 'string', key: '驾驶员', value: event.value.driver },
    { type: 'int', key: '处理状态', value: event.value.statusnew },
    { type: 'int', key: 'mpLayer', value: 0 },
  ]
  const status = event.value.statusnew
  // console.log(position);
  Point.add({
    position,
    attr,
    service,
    docLayer:docLayer,
    status,
  })
  docLayer.refresh()
  //关闭窗口
  store.xgsjDialog = false
  popvisible.value=false
  map.un('click',xgsjpop)
  source.clear()
  store.activeIndex = '0'
}
const handleCancel=()=>{
  store.xgsjDialog = false
  popvisible.value=false
  map.un('click',xgsjpop)
  source.clear()
  store.activeIndex = '0'
  docLayer.refresh()
}
</script>

<style scoped>
.popup {
  position: absolute;
  width: 300px;
  height: 360px;
  z-index: 500;
  background-color: white;
  border: 1px solid grey;
  border-radius: 10px;
  padding: 10px 10px;
  transition: all 0.5s;
}
.dialog-footer {
  display: flex;
  justify-content: space-evenly;
}
</style>
